// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

var NetworkManager = require('NetworkManager')

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        initTotal: 1000,    // 初始化进度
        msg: cc.Label,
        progressBar: cc.ProgressBar
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.msg.string = '连接大厅服务器...'
        if (NetworkManager.gHallServer === null) {
            NetworkManager.connectAndAuthToHall()
        }
        NetworkManager.onConnectedToHall(() => {
            this.msg.string = '连接大厅服务器成功...'
            cc.director.preloadScene('hall',
                this.onProgress.bind(this), () => {
                if (this.msg !== null) {
                    this.msg.string = '静态资源加载成功...'
                }
            })
            cc.director.loadScene('hall')
        })
    },

    // 进度条
    onProgress (com, total, item) {
        if (total < 10) {
            total = this.initTotal
        }

        let progress = com / total
        this.progressBar.progress = progress

        this.msg.string = '静态资源加载中('+ (progress*100).toFixed(2) +'%)...'
    }
});
